<?php
apf_require_class('APF_Controller');
/**
 *
 * @author 吕嗣江
 *
 */
class Oa_Attendance_CrmAPIController extends APF_Controller {

    /**
     * 请求什么数据(state:某天请假的人工时数,user:员工入职信息,workday:指定日期的定义)
     * @var string (state|user|workday)
     */
    private $for_what;


    /**
     * 请求的参数
     * @var string
     */
    private $param;


    private $data;


    /**
     * 参数错误信息
     * @var string
     */
    CONST INVALID_PARAM_MSG = '参数错误！';


    public function handle_request(){
        //检测参数
        $this->check_param();

        //获取数据
        $this->get_data();

        $status=1;
        if(empty($this->data)){
            $status=0;
        }
        $this->echo_json($this->data,$status);
        die;
    }


    /**
     * 检查参数
     */
    private function check_param(){
        $request = APF::get_instance()->get_request();
        $matches = $request->get_router_matches();
        $this->for_what = $matches[1];
        $params = $request->get_parameters();
        switch( $this->for_what ){
            case 'state':
                if( !is_numeric( $params['date'] ) ){
                    $this->echo_json(self::INVALID_PARAM_MSG, 0);die;
                }else{
                    $this->param = $params['date'];
                }
                break;
            case 'workday':
                if( !is_numeric( $params['year'] ) ){
                    $this->echo_json(self::INVALID_PARAM_MSG, 0);die;
                }else{
                    $this->param = $params['year'];
                }
                break;
            case 'user':
                if(!is_string($params['code'])){
                    $this->echo_json(self::INVALID_PARAM_MSG, 0);die;
                }else{
                    $this->param = base64_decode($params['code']);
                }
                break;
        }
        return;
    }


    /**
     * 获取数据
     */
    private function get_data(){
        switch( $this->for_what ){
            case 'state':
                $this->get_leave_hours();//请假的人工时数
                break;
            case 'workday':
                $this->get_workday();//指定日期的定义
                break;
            case 'user':
                $this->get_enter_date();//员工入职信息
                break;
        }
        return;
    }


    /**
     * 获取当天请假的人工时数
     */
    private function get_leave_hours(){
        apf_require_class('Bll_Attendance_DailyLeave');
        $bll_dl = new Bll_Attendance_DailyLeave();
        $this->data = $bll_dl->get_leave_hours_by_date( $this->param );
        return;
    }


    /**
     * 获取指定日期的定义
     */
    private function get_workday(){
        apf_require_class('Bll_Attendance_SpecialDay');
        $bll_sd = new Bll_Attendance_SpecialDay();
        $this->data = $bll_sd->get_sd_by_year( $this->param );
        return;
    }


    /**
     * 获取员工入职日期
     */
    private function get_enter_date(){
        apf_require_class('Bll_Attendance_UserInfo');
        $bll_user = new Bll_Attendance_UserInfo();
        $enter_date = $bll_user->get_enterdate_by_code( $this->param );
        if( empty($enter_date) ){
            $this->data = array();
            return;
        }else{
            //将日期中的-去掉
            foreach( $enter_date as &$v ){
                $v = array($v['code']=>(int)str_replace('-', '', substr($v['enter_date'], 0, 10)));
            }
            $this->data = $enter_date;
            return;
        }
    }

    private function echo_json( $data, $status=1 ){
        echo json_encode(array('status'=>$status, 'data'=>$data));
    }
}